package com.leo;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 * @ClassName DynamicChartDemo
 * @Description 动态chart
 * @Author wangss
 * @date 2020.06.30 22:46
 * @Version 1.0
 */
public class DynamicChartDemo extends Application {

    private static final int max = 100;
    private static final int maxLength = 18;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        AnchorPane an = new AnchorPane();

        Button play = new Button("监控");
        Button stop = new Button("暂停");

        HBox hBox = new HBox(20);
        hBox.getChildren().addAll(play, stop);

        LineChart<Number, Number> lineChart = createChart();

        AnchorPane.setTopAnchor(hBox, 50d);
        AnchorPane.setLeftAnchor(hBox, 50d);
        AnchorPane.setTopAnchor(lineChart, 80d);
        an.getChildren().addAll(hBox, lineChart);

        Scene scene = new Scene(an);
        primaryStage.setScene(scene);
        primaryStage.setTitle("动态chart");
        primaryStage.setWidth(900);
        primaryStage.setHeight(800);
        primaryStage.show();

        MyScheduledService service = new MyScheduledService();
        service.setDelay(Duration.seconds(0));
        service.setPeriod(Duration.seconds(1));
        service.valueProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue != null && !newValue.isEmpty()) {
                int value1 = newValue.get(0);
                int value2 = newValue.get(1);
                NumberAxis x = (NumberAxis) lineChart.getXAxis();
                XYChart.Series<Number, Number> xy1 = lineChart.getData().get(0);
                XYChart.Series<Number, Number> xy2 = lineChart.getData().get(1);
                int index = xy1.getData().size();

                // 每展示50条数据，就清空重新在加载
                if (index >= max) {
                    index = 0;
                    xy1.getData().clear();
                    xy2.getData().clear();
                    // 设置x轴的最小值和最大值
                    x.setLowerBound(0);
                    x.setUpperBound(20);
                }

                if (index > maxLength) {
                    x.setLowerBound(x.getLowerBound() + 1);
                    x.setUpperBound(x.getUpperBound() + 1);
                }
                xy1.getData().add(new XYChart.Data<>(index, value1));
                xy2.getData().add(new XYChart.Data<>(index, value2));
            }
        });

        play.setOnAction(event -> {
            if (!service.isRunning()) {
                service.start();
            }
        });

        stop.setOnAction(event -> {
            if (service.isRunning()) {
                service.cancel();
                service.reset();
            }
        });

    }

    private LineChart<Number, Number> createChart() {
        NumberAxis x = new NumberAxis("x轴", 0, 20, 1);
        NumberAxis y = new NumberAxis("y轴", 0, 100, 10);

        LineChart<Number, Number> lineChart = new LineChart<>(x, y);
        lineChart.setPrefWidth(800d);
        lineChart.setPrefHeight(500d);
        lineChart.setAnimated(false);

        XYChart.Series<Number, Number> xy1 = new XYChart.Series<>();
        xy1.setName("xy1");
        XYChart.Series<Number, Number> xy2 = new XYChart.Series<>();
        xy2.setName("xy2");

        lineChart.getData().addAll(xy1, xy2);

        return lineChart;
    }
}
